var visualizzato;
var message = false;
$(document).ready(function(){
  $(".gotop").click(function(){
    $("html, body").animate({
      scrollTop:$("#container-faq-domande").offset().top-widthTrans-10
    },500);
  });
  
  $(".internalLink").click(function(){
    var whereToGo = $(this).attr("goto");
    $("html, body").animate({
      scrollTop:$("#question"+whereToGo).offset().top-widthTrans-10
    },500,function(){
      $("#question"+whereToGo).animate({backgroundColor:"yellow"},200,function(){
       $(this).animate({backgroundColor: '#ffffff'})
      });
    });
  });
  
  $("#askaquestion").click(function(){
    if(message == false)
      message = $("#domanderisposteForm").html();
    $("#domanderisposteForm").empty();
    Dialog(message);
    formscript("blockMsg");
    });
    $("body").keyup(function(e){
          if(e.which == 27)
            $(".blockOverlay").trigger("click");
        });
});
var resetRadioCampioneAdmin = false;
function aumenta(elem,ret){
  ret = ret || "abc";
  if(ret == "abc" && resetRadioCampioneAdmin == true) return;
  if(ret == "abc" && resetRadioCampioneAdmin == false)
    resetRadioCampioneAdmin=true;
  if(ret != "abc")
    resetRadioCampioneAdmin=false;
   if(visualizzato == "si"){
     visualizzato = "no";
     $(".richiediCampione").fadeOut(100,function(){
       $(".blockMsg").animate({
        "left": ($(window).width() - 433)/2
      },500);
      $(".alert").animate({
        "width":"433px"
      });
      $(".alert .chiudi").animate({
       "left":400
     });
     })
     
   }
   else{
     visualizzato = "si";
      $(".alert").animate({
        "width":"810px"
      },500,function(){
        $(".richiediCampione").fadeIn();
      });
      $(".alert .chiudi").animate({
       "left":780
     },510);
      $(".blockMsg").animate({
        "left": ($(window).width() - 810)/2
      });
    }
    }


function inviaFormCampione(isadmin){
  isadmin = isadmin || "abc";
  var blurl = getBaseURL();
    var dati = "";
    if(visualizzato == "si"){
      dati = $("#faiunadomandaForm").serialize();
      dati = dati+"&"+$("#richiedicampioneForm").serialize();
    }
    else{
      dati = $("#faiunadomandaForm").serialize();
    }
    if(isadmin != "abc")
      dati = dati+"&username="+$("#username").attr("value")+"&email="+escape($("#email").attr("value"))+"&nome="+escape($("#nome").attr("value"))+"&cognome="+escape($("#cognome").attr("value"));
    var action = "askForTester";
      $.ajax({
        type: 'POST',
        data: dati,
        url: blurl+"/wp-admin/admin-ajax.php?action="+action,
        success: function(data){
            if(data == "ok"){
              Dialog("La tua richiesta sarà processata al più presto!");
              return;
            }
            if(data == "campione"){
              Dialog("Hai già richiesto il campione di prova!");
              return;}

            for (var property in data) { 		
                $("#"+property+"-err").html(eval("data."+property));
            }
            
        },
        error: function(data){
        }

    }); 
    }



function Dialog(message){
  $.blockUI.defaults.css = {
        cursor:"normal",
        left: ($(window).width() - 433)/2,
        top: ($(window).height() - 433)/2

    };
    $.blockUI.defaults.overlayCSS =  { 
        backgroundColor: '#000', 
        opacity:         0.6,
        cursor: "normal"
    }, 
    $.blockUI({
      message: '<div class="alert"><div class="chiudi" onclick="$(\'.blockOverlay\').trigger(\'click\');"><a href="#"></a></div><div class="boxGradiente gradient">'+message+'</div></div>',
      focusInput: false,
      onBlock: function(){
        
        $('.blockOverlay').attr('title','Click to unblock').click(function(){
          $.unblockUI();
          visualizzato = false;
          resetRadioCampioneAdmin = false;
        });
      }
      });
}

function segnaComeInviato(elem,id){
  var blurl = getBaseURL();
  $.ajax({
        type: 'POST',
        data: "inviato=true&id="+id,
        url: blurl+"/wp-admin/admin-ajax.php?action=testerSent",
        success: function(response){
           if(response == "ok")
             $(elem).parent().parent().fadeOut();
        }
    }); 
}


function getBaseURL() {
    var url = location.href;  // entire url including querystring - also: window.location.href;
    var baseURL = url.substring(0, url.indexOf('/', 14));


    if (baseURL.indexOf('http://localhost') != -1) {
        // Base Url for localhost
        var url = location.href;  // window.location.href;
        var pathname = location.pathname;  // window.location.pathname;
        var index1 = url.indexOf(pathname);
        var index2 = url.indexOf("/", index1 + 1);
        var baseLocalUrl = url.substr(0, index2);

        return baseLocalUrl + "/";
    }
    else {
        // Root Url for domain name
        return baseURL + "/";
    }

}
